<?php
/**
 * Created by : PhpStorm
 * User: Shane
 * Date: 2023-12-18
 * Time: 14:26
 */

namespace addons\shopro\job;


use addons\shopro\exception\ShoproException;
use think\Db;
use think\exception\HttpResponseException;
use think\queue\Job;

class GoodsPrize extends BaseJob
{
    /**
     * @param Job $job
     * @param $data
     * 有奖商品开奖
     */
    public function openPrize(Job $job,$data){
        try{
            $goods_id=$data["goods_id"];//有奖商品ID
            $open_prize_time=$data["open_prize_time"];//开奖时间
            $goods=Db::name("shopro_goods")->where([
                "id"=>$goods_id,
                "is_prize"=>"yes",
                "open_prize_time"=>$open_prize_time
            ])->field("id,first_prize_sum,second_prize_sum,third_prize_sum")->find();
            if($goods){
                $waitOpenPrizeCount=Db::name("westts_goods_prize")->where([
                    "goods_id"=>$goods_id,
                    "open_prize_status"=>"wait",
                ])->count();
                $prizeTotal=bcadd($goods["third_prize_sum"],bcadd($goods["first_prize_sum"],$goods["second_prize_sum"]));
                $prizeArr=[];//中奖号码数组
                if($waitOpenPrizeCount > 0 && $waitOpenPrizeCount >= $prizeTotal){
                    $prizeArr=unique_rand(0,bcsub($waitOpenPrizeCount,1),$prizeTotal);
                }elseif ($waitOpenPrizeCount > 0 && $waitOpenPrizeCount < $prizeTotal){
                    $prizeArr=unique_rand(0,bcsub($waitOpenPrizeCount,1),$waitOpenPrizeCount);
                }
                if(count($prizeArr)>0){
                    $firstPrizeArr=[];//一等奖中奖数组
                    $secondPrizeArr=[];//二等奖中奖数组
                    $thirdPrizeArr=[];//三等奖中奖数组
                    foreach ($prizeArr as $k=>$v){
                        //由发一等奖依次向下发
                        if(count($firstPrizeArr) < $goods["first_prize_sum"]){
                            $firstPrizeArr[]=$v;
                        }elseif (count($secondPrizeArr) < $goods["second_prize_sum"]){
                            $secondPrizeArr[]=$v;
                        }elseif(count($thirdPrizeArr) < $goods["third_prize_sum"]){
                            $thirdPrizeArr[]=$v;
                        }
                    }
                    //一等奖发奖
                    if(count($firstPrizeArr)>0){
                        $this->updatePrizeData("first",$firstPrizeArr,$goods_id);
                    }
                    //二等奖发奖
                    if(count($secondPrizeArr)>0){
                        $this->updatePrizeData("second",$secondPrizeArr,$goods_id);
                    }
                    //三等奖发奖
                    if(count($thirdPrizeArr)>0){
                        $this->updatePrizeData("third",$thirdPrizeArr,$goods_id);
                    }
                    //未中奖
                    $this->updatePrizeData("wait",[],$goods_id);
                }
            }
            // 删除 job
            $job->delete();
        }catch (ShoproException $e){
            // 自定义异常时删除 队列
            $job->delete();

            format_log_error($e, 'GoodsPrize.openPrize.sheepException');
        }catch (HttpResponseException $e){
            // error_stop 异常时删除 队列
            $job->delete();

            $data = $e->getResponse()->getData();
            $message = $data ? ($data['msg'] ?? '') : $e->getMessage();
            format_log_error($e, 'GoodsPrize.openPrize.HttpResponseException', $message);
        }catch (\Exception $e){
            // 队列执行失败
            format_log_error($e, 'GoodsPrize.openPrize');
        }
    }

    /**
     * @param string $type
     * @param array $prizeArr
     * @param string $goods_id
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     * 更新开奖数据
     */
    private function updatePrizeData($type="wait",$prizeArr=[],$goods_id=""){
        $idsArr=[];
        $time=date("Y-m-d H:i:s");
        if($prizeArr){
            foreach ($prizeArr as $startLocal){
                $id=Db::name("westts_goods_prize")
                    ->where(["goods_id"=>$goods_id,"open_prize_status"=>"wait"])
                    ->limit($startLocal,1)->column("id");
                $idsArr[]=$id[0];
            }
        }
        if(count($idsArr)>0) Db::name("westts_goods_prize")
            ->where(["id"=>["in",$idsArr]])
            ->update(["open_prize_status"=>$type,"open_prize_time"=>$time]);

        if($type=="wait"){
            //将其他状态全部更新为未中奖
            Db::name("westts_goods_prize")
                ->where(["goods_id"=>$goods_id,"open_prize_status"=>"wait"])
                ->update(["open_prize_status"=>"no","open_prize_time"=>$time]);
        }
    }
}